﻿using System;
using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;

namespace Mekiasm
{
    public class Crusher : MachineRotateAbleItem, Interface.IDynamicCraft
    {
        public static int Index;

        public int[] FuelCraftSlotIndies => new int[0];

        public int[] CraftSlotIndies => new int[] { 0 };

        public int[] ResultSlotIndies => new int[] { 1 };

        public int[] RemainSlotIndies => new int[0];

        public int SlotsCount => 2;

        public int BlockValue => blockValue;

        public Point2 CraftSize => new Point2(1);

        public Crusher() : base( "粉碎机")
        {
            
            textureSlots = new int[] { 63, 79, 47, 79, 95, 95 };
        }
        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }

        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z,
                new List<ComponentEnergyMachine.Slot>() { new ComponentEnergyMachine.Slot(), new ComponentEnergyMachine.Slot() },
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item)
            }),new MachineAmountLimitConfig() { InputPowerLimit = 32,OutputPowerLimit = 32,MaxPower = 10240});
            ElectricSystem.Add(new CrusherElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new CrusherElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ElectricSystem.Remove(new Point3(x, y, z));
            ILibrary.RemoveBlockEntity(x, y, z);
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentCrusher mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentCrusher>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new CrusherWidget(mcomponent, componentMiner);
            return true;
        }
        public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
        {
            return ILibrary.GetPlacementValue(subsystemTerrain, componentMiner, value, raycastResult);
        }

        public void GenerateRecipes(List<MekRecipe> mekRecipe)
        {
            mekRecipe.Add(new MekRecipe(1).SetName("沙子").SetIngredian(0, ILibrary.GetCraftId(CobblestoneBlock.Index)).SetResult(0, SandBlock.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("沙子").SetIngredian(0, ILibrary.GetCraftId(SandstoneBlock.Index)).SetResult(0, SandBlock.Index, 2));
            mekRecipe.Add(new MekRecipe(1).SetName("煤粉").SetIngredian(0, ILibrary.GetCraftId(CoalChunkBlock.Index)).SetResult(0, CoalDust.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("铁粉").SetIngredian(0, ILibrary.GetCraftId(IronIngotBlock.Index)).SetResult(0, IronDust.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("钢粉").SetIngredian(0, ILibrary.GetCraftId(SteelIgnot.Index)).SetResult(0, SteelDust.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("锡粉").SetIngredian(0, ILibrary.GetCraftId(TinIngnot.Index)).SetResult(0, TinDust.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("铜粉").SetIngredian(0, ILibrary.GetCraftId(CopperIngotBlock.Index)).SetResult(0, CopperDust.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("钻石粉").SetIngredian(0, ILibrary.GetCraftId(DiamondChunkBlock.Index)).SetResult(0, DiamondDust.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("锇粉").SetIngredian(0, ILibrary.GetCraftId(OreOsmium.Index)).SetResult(0, OsmiumDust.Index));
        }

        public void GenerateRecipeWidget(CanvasWidget canvas, ComponentEnergyMachine energyMachine, ComponentMiner miner, ref Action Tick)
        {
            InventorySlotWidget inventory, inventory1;
            StackPanelWidget mStackCenter = new StackPanelWidget() { Direction = LayoutDirection.Vertical, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
            MekProgressWidget craftProgress = new MekProgressWidget(MekProgressWidget.ProgessType.down) { HorizontalAlignment = WidgetAlignment.Center, Size = new Vector2(8, 50), Margin = new Vector2(0, 10) };
            inventory = new InventorySlotWidget() { Size = new Vector2(48, 48), BevelColor = Color.Black };
            inventory1 = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(0, 10) };
            inventory.AssignInventorySlot(energyMachine, 0);
            inventory1.AssignInventorySlot(energyMachine, 1);
            mStackCenter.AddChildren(inventory);
            mStackCenter.AddChildren(craftProgress);
            mStackCenter.AddChildren(inventory1);
            canvas.AddChildren(mStackCenter);
            Tick += () => {
                craftProgress.SetProgress(energyMachine.CraftProgress);
            };
        }
    }
}
